package com.yonyou.pmclouds.projecttype.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeMaintain;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeOutterMaintain;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeQuery;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectTypeOutterMaintain.class)
public class ProjectTypeOutterMaintainImpl implements ProjectTypeOutterMaintain {
    //需要更新的字段
    private static final String[] OUTTER_UPDATE_FIELDS = {ProjectTypeVO.PK_PROJECT_TYPE,ProjectTypeVO.SNAME,
            ProjectTypeVO.SCODE,ProjectTypeVO.PK_TENANT,ProjectTypeVO.PK_SRC_ORG,ProjectTypeVO.PK_SRC,
            ProjectTypeVO.ENABLE_STATUS, CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME};

    @Autowired
    private ProjectTypeMaintain projectTypeMaintain;
    @Autowired
    private ProjectTypeQuery projectTypeQuery;

    @Override
    public ProjectTypeVO insertFromOutter(ProjectTypeVO projectTypeVO) throws BusinessException {
        ProjectTypeVO origVO = projectTypeQuery.queryByPkSrc(projectTypeVO.getPkSrc());
        if(origVO != null)
            return origVO;
        return projectTypeMaintain.insert(projectTypeVO);
    }

    @Override
    public String deleteFromOutter(ProjectTypeVO projectTypeVO) throws BusinessException {
        ProjectTypeVO origVO = projectTypeQuery.queryByPkSrc(projectTypeVO.getPkSrc());
        return projectTypeMaintain.delete(origVO.getPkProjectType(), origVO.getChangeFlag());
    }

    @Override
    public ProjectTypeVO updateFromOutter(ProjectTypeVO projectTypeVO) throws BusinessException {
        ProjectTypeVO origVO = projectTypeQuery.queryByPkSrc(projectTypeVO.getPkSrc());

        if(origVO == null){
            return projectTypeMaintain.insert(projectTypeVO);
        }else{
            origVO.setScode(projectTypeVO.getScode());
            origVO.setSname(projectTypeVO.getSname());
            origVO.setEnableStatus(projectTypeVO.getEnableStatus());

            projectTypeMaintain.update(origVO);

            return origVO;
        }

    }

    @Override
    public ProjectTypeVO enable(ProjectTypeVO projectTypeVO) throws BusinessException {
        ProjectTypeVO origVO = projectTypeQuery.queryByPkSrc(projectTypeVO.getPkSrc());
        ProjectTypeVO enable=null;
        if (origVO!=null){
            enable = projectTypeMaintain.enable(projectTypeVO);
        }
        return enable;
    }

    @Override
    public ProjectTypeVO[] disEnable(ProjectTypeVO projectTypeVO) throws BusinessException {
        ProjectTypeVO origVO = projectTypeQuery.queryByPkSrc(projectTypeVO.getPkSrc());
        ProjectTypeVO[] disEnable=new ProjectTypeVO[0];
        if (origVO!=null){
           disEnable = projectTypeMaintain.disEnable(projectTypeVO);
        }
        return disEnable;
    }
}
